<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>考勤管理</el-breadcrumb-item>
      <el-breadcrumb-item>加班表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row :gutter="20">
        <el-col :span="6">
          <el-input placeholder="申请人" clearable v-model="eName" @clear="searchOverTime">
            <el-button slot="append" icon="el-icon-search" @click="searchOverTime"></el-button>
          </el-input>
        </el-col>
        <el-col :span="6">
          <el-input placeholder="加班类型" clearable v-model="type" @clear="searchOverTime">
            <el-button slot="append" icon="el-icon-search" @click="searchOverTime"></el-button>
          </el-input>
        </el-col>
        <el-col :span="1">
          <el-button type="primary" @click="addOverTimeFormVisible=true">添加加班记录</el-button>
        </el-col>
      </el-row>

      <el-table :data="overTimeList" border stripe>
        <el-table-column type="index"></el-table-column>
        <el-table-column label="员工编号" prop="eId" ></el-table-column>
        <el-table-column label="员工姓名" prop="eName" ></el-table-column>
        <el-table-column label="加班类型" prop="type" ></el-table-column>
        <el-table-column label="开始时间" prop="startTime" ></el-table-column>
<!--        <el-date-picker prop="startTime" type="datetime" placeholder="选择日期时间"></el-date-picker>-->
        <el-table-column label="结束时间" prop="endTime" ></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope" >
                        <el-button type="warning" size="mini" @click="showEditDialog(scope.row)">编辑</el-button>
            <el-button type="danger" size="mini" @click="removeById(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-sizes="[2, 4 , 8, 16]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
      ></el-pagination>
    </el-card>
    <div>
      <el-dialog title="添加加班记录" :visible.sync="addOverTimeFormVisible" :close-on-click-modal="false">
        <el-form ref="addChannelFormRef" :rules="addOverTimeFormRules" :model="addOverTimeForm" label-width="80px" size="mini">

          <el-form-item label="员工工号" prop="eId">
            <el-input v-model="addOverTimeForm.eId"></el-input>
          </el-form-item>
          <el-form-item label="员工姓名" prop="eName">
            <el-input v-model="addOverTimeForm.eName"></el-input>
          </el-form-item>

          <el-form-item label="加班类型" prop="type">
            <el-input v-model="addOverTimeForm.type"></el-input>
          </el-form-item>

          <el-form-item label="开始时间" prop="startTime">
            <el-date-picker
                v-model="addOverTimeForm.startTime"
                type="datetime"
                value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="选择日期时间">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="结束时间" prop="endTime">
            <el-date-picker
                v-model="addOverTimeForm.endTime"
                type="datetime"
                value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="选择日期时间">
            </el-date-picker>
          </el-form-item>
        </el-form>

        <div slot="footer" class="dialog-footer">
          <el-button @click="cancelAddOverTimeDialog">取 消</el-button>
          <el-button type="primary" @click="addOverTime">确 定</el-button>
        </div>
      </el-dialog>


      <el-dialog title="修改加班记录" :visible.sync="modifyOverTimeFormVisible" :close-on-click-modal="false">
        <el-form ref="addChannelFormRef" :rules="modifyOverTimeFormRules" :model="modifyOverTimeForm" label-width="80px" size="mini">

          <el-form-item label="员工工号" prop="eId">
            <el-input v-model="modifyOverTimeForm.eId" disabled></el-input>
          </el-form-item>
          <el-form-item label="员工姓名" prop="eName">
            <el-input v-model="modifyOverTimeForm.eName" disabled></el-input>
          </el-form-item>

          <el-form-item label="加班类型" prop="type">
            <el-input v-model="modifyOverTimeForm.type"></el-input>
          </el-form-item>

          <el-form-item label="开始时间" prop="startTime">
            <el-date-picker
                v-model="modifyOverTimeForm.startTime"
                type="datetime"
                value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="选择日期时间">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="结束时间" prop="endTime">
            <el-date-picker
                v-model="modifyOverTimeForm.endTime"
                type="datetime"
                value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="选择日期时间">
            </el-date-picker>
          </el-form-item>
        </el-form>

        <div slot="footer" class="dialog-footer">
          <el-button @click="cancelModifyOverTimeDialog">取 消</el-button>
          <el-button type="primary" @click="modifyOverTime">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
export default {
  name: "OverTime",
  data(){
    return{
      eId:'',
      eName:'',
      type:'',
      overTimeList:[],
      addOverTimeFormVisible:false,
      pageNum:1,
      pageSize:2,
      total:0,

      //添加表单
      addOverTimeForm:{
        eId:'',
        eName:'',
        type:'',
        startTime:'',
        endTime:'',
      },
      addOverTimeFormRules:{

      },
      //修改表单
      modifyOverTimeForm:{
        eId:'',
        eName:'',
        type:'',
        startTime:'',
        endTime:'',
      },
      modifyOverTimeFormVisible:false,
      modifyOverTimeFormRules:{

      },

    }
  },
  methods:{
    searchOverTime(){
      this.initOverTime();
    },
    initOverTime(){
      let url = this.$HRMSUrl + "/overTime/overTimeList";
      console.log(this.eName)
      this.$axios.get(url,{
        params:{
          eName:this.eName,
          type:this.type,
          now:this.pageNum,
          size:this.pageSize
        }
      }).then(res=>{
        if (res.data.code == 200){
          this.overTimeList = res.data.data.list;
          this.pageNum = res.data.data.pageNum;
          this.pageSize = res.data.data.pageSize;
          this.total = res.data.data.total;
        }else {
          this.$message({
            type:"error",
            message:res.data.msg,
            duration:1000
          })
        }
      }).catch(error=>{
        this.$message({
          type:"error",
          message:"系统错误，请联系管理员！",
          duration:1000
        })
      })
    },
    showEditDialog(row){
      this.modifyOverTimeForm=row;
      this.modifyOverTimeFormVisible=true;
    },
    addOverTime(){
      let url = this.$HRMSUrl + "/overTime/add";
      console.log(this.addOverTimeForm)
      this.$axios.post(url,this.addOverTimeForm).then(
          res=>{
            if (res.data.code == 200){
              this.addOverTimeFormVisible = false;
              this.addOverTimeForm = {};
              this.initOverTime();
            }else {
              this.$message({
                type:"error",
                message:res.data.msg,
                duration:1000
              })
            }
          }).catch(error=>{
        this.$message({
          type:"error",
          message:"系统错误，请联系管理员!",
          duration:1000
        })
      })
    },
    cancelAddOverTimeDialog(){
    this.addOverTimeFormVisible = false;
    this.addOverTimeForm = {}
    },
    removeById(id){
      let url = this.$HRMSUrl+"/overTime/del/"+id;
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$axios.delete(url)
            .then(res=>{
              if (res.data.code == 200){
                this.initOverTime();
              }else {
                this.$message({
                  type:"error",
                  message:res.data.msg,
                  duration:1000
                })
              }
            })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    handleSizeChange(size){
      this.pageSize = size;
      this.initOverTime();
    },
    handleCurrentChange(now){
      this.pageNum = now;
      this.initOverTime();
    },
    cancelModifyOverTimeDialog(){
      this.modifyOverTimeFormVisible = false;
    },
    modifyOverTime(){
      let url = this.$HRMSUrl+"/overTime/modify";
      this.$axios.put(url,this.modifyOverTimeForm)
          .then(res=>{
            if (res.data.code == 200){
              this.modifyOverTimeFormVisible = false ;
              this.initOverTime();
            }else {
              this.$message({
                type:"error",
                message:res.data.msg,
                duration:1000
              })
            }
          }).catch(error=>{
        this.$message({
          type:"error",
          message:"系统错误，请联系管理员!",
          duration:1000
        })
      })
    },
  },
  created() {
    this.initOverTime();
  }
}
</script>

<style scoped>

</style>